package com.admin.kotlintest.config

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.os.Binder
import android.os.StrictMode

import cn.bmob.v3.Bmob
import android.os.StrictMode.setVmPolicy
import android.support.multidex.MultiDex
import cn.bmob.push.BmobPush
import cn.bmob.v3.BmobInstallationManager.getInstallationId
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.BmobInstallation
import cn.bmob.v3.InstallationListener
import cn.bmob.v3.BmobInstallationManager
import com.socks.library.KLog


/**
 * Created by 吴昶 on 2018/3/2.
 */

class MyApplication : Application() {
    public var context: Context? = null
    override fun onCreate() {
        super.onCreate()
        Bmob.initialize(this, "b53515b2da4bdc082963bcc94aff3e5d")
        context = applicationContext
        initSharedPreferences()
        //确保调用相机的时候，系统可以访问file://的uri的地址
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        builder.detectFileUriExposure()

        BmobInstallationManager.getInstance().initialize(object : InstallationListener<BmobInstallation>() {
            override fun done(bmobInstallation: BmobInstallation, e: BmobException?) {
                if (e == null) {
                    KLog.d(bmobInstallation.objectId + "-" + bmobInstallation.installationId)
                } else {
                    KLog.e(e.message)
                }
            }
        })
        // 启动推送服务
        BmobPush.startWork(this)
    }
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    /**
     * 初始化SharedPreferences
     */
    private fun initSharedPreferences() {
        if (sharedPreferences == null) {
            sharedPreferences = context!!.getSharedPreferences("kotlintest", Activity.MODE_PRIVATE)
        }
    }

    companion object {
        private var sharedPreferences: SharedPreferences? = null
        var activityList= ArrayList<Activity>()
        /**
         * SharedPreferences 存储int值
         * @param key
         * @param value
         */
        fun setSharedPreferencesInteger(key: String, value: Int) {
            val editor = sharedPreferences!!.edit()
            editor.putInt(key, value)
            editor.commit()
        }

        fun getSharedPreferencesInteger(key: String, value: Int): Int {
            return sharedPreferences!!.getInt(key, value)
        }

        /**
         * SharedPreferences 存储字符串
         * @param key
         * @param value
         */
        fun setSharedPreferencesString(key: String, value: String) {
            val editor = sharedPreferences!!.edit()
            editor.putString(key, value)
            editor.commit()
        }

        fun getSharedPreferencesString(key: String, value: String): String? {
            return sharedPreferences!!.getString(key, value)
        }

        /**
         * SharedPreferences 存储boolean值
         * @param key
         * @param value
         */
        fun setSharedPreferencesBoolean(key: String, value: Boolean) {
            val editor = sharedPreferences!!.edit()
            editor.putBoolean(key, value)
            editor.commit()
        }

        /**
         * @param key
         * @param value
         * @return
         */
        fun getSharedPreferencesBoolean(key: String, value: Boolean): Boolean {
            return sharedPreferences!!.getBoolean(key, value)
        }

        fun addActivityManager(activity:Activity){
            activityList.add(activity)
        }

        fun finishAllActivity(){
            (0 until activityList.count()).forEach {
                activityList[it].finish()
            }
        }
    }

}
